<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>AJAX测试</title>
</head>
<body>

<button type="button" onclick="getData()">获取普通文本</button>
<script>
  var xhr;

  function getData() {
    xhr = new XMLHttpRequest();
    xhr.open("POST", "AJAXServlet?cmd=1", true)
    xhr.onreadystatechange = showData
    xhr.send(null)
  }

  function showData() {
    if (xhr.readyState == 4 && xhr.status == 200) {
      var text = xhr.responseText;
      console.info(text)
    }
  }

</script>

<button type="button" onclick="jsonData()">获取json格式的文本</button>
<script>
  var xhr;

  function jsonData() {
    xhr = new XMLHttpRequest();
    xhr.open("POST", "AJAXServlet?cmd=2", true);
    xhr.onreadystatechange = showReturnInfo;
    xhr.send(null);
  }

  function showReturnInfo() {
    if (xhr.readyState == 4 && xhr.status == 200) {
      var info = xhr.responseText;
      var entities = JSON.parse(info)
      for (var i = 0; i < entities.length; i++) {
        console.log("当前是第" + i + "次循环")
        var entity = entities[i];
        console.log("id: " + entity.id)
        console.log("description: " + entity.description)
        console.log("type: " + entity.type)
        console.log("createTime: " + entity.createTime)
      }
    }
  }
</script>


</body>
</html>
